﻿using System.ComponentModel;
using System.Linq;
using DevExpress.XtraEditors;
using KPFramework.Base;
using KPFramework.Demo.Properties;
using KPFramework.KUIFrame;

namespace KPFramework.Demo.Components
{
    [Browsable(false)]
    [ToolboxItem(false)]
    internal partial class HomePageCtrl : XtraUserControl
    {
        public HomePageCtrl()
        {
            InitializeComponent();
        }



        #region 事件处理
        private void itemBase_ItemClick(object sender, TileItemEventArgs e)
        {
            OnShowUi(e.Item);
        }

        private void itemWinForm_ItemClick(object sender, TileItemEventArgs e)
        {
            OnShowUi(e.Item);
        }

        private void itemApp_ItemClick(object sender, TileItemEventArgs e)
        {
            OnShowUi(e.Item);
        }
        #endregion


        #region 功能方法
        /// <summary>
        /// 创建ui功能
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        private KUIObject OnGetUi(string strName)
        {
            var items = new []
            {
                new { Name = "KP.Demo.BaseUi",Desc = "框架与应用",Service = "KPDemo.BaseService",Image = Resources.project_32x32},
                new { Name = "KP.Demo.WinForm",Desc = "WinForm常用功能",Service = "KPDemo.WinFormService",Image = Resources.csharp_16x16},
                new { Name = "KP.Demo.App",Desc = "综合案例",Service = "KPDemo.ExamplesService",Image = Resources.solution_32x32},
            };

            var item = items.FirstOrDefault((a) => a.Name == strName);
            if (item == null)
            {
                CommonCtrl.MsgBox.KMsgBox.Warn("未获得实现组件.");
                return null;
            }
            return new KUIObject()
            {
                Name = strName,
                Desc = item.Desc,
                Title = item.Desc,
                CtrlService =  item.Service,
                FHT = E_FunctionHostType.FHT_Service,
                Image = item.Image,
                TypeCode = item.Name
            };

        }

        /// <summary>
        /// 显示组件
        /// </summary>
        /// <param name="item"></param>
        private void OnShowUi(TileItem item)
        {
            if(item == null)
                return;
            string strValue = item.Tag?.ToString();
            var ui = OnGetUi(strValue);
            if (ui == null)
                return;
            UIFrame.ShowUi(ui);
        }
        #endregion
    }


    internal class HomePageUi : UiCommponent<HomePageCtrl>
    {
        public HomePageUi(KUIObject uiInfo):base(uiInfo)
        {

        }
    }
}
